home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / StringBufferInputStream.java < prev    next >
Text File  |  1998-09-22  |  5KB  |  167 lines

  1. /*
  2.  * @(#)StringBufferInputStream.java    1.17 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * This class allows an application to create an input stream in 
  19.  * which the bytes read are supplied by the contents of a string. 
  20.  * Applications can also read bytes from a byte array by using a 
  21.  * <code>ByteArrayInputStream</code>. 
  22.  * <p>
  23.  * Only the low eight bits of each character in the string are used by
  24.  * this class. 
  25.  *
  26.  * @author     Arthur van Hoff
  27.  * @version    1.17, 07/01/98
  28.  * @see        java.io.ByteArrayInputStream
  29.  * @see        java.io.StringReader
  30.  * @since      JDK1.0
  31.  * @deprecated This class does not properly convert characters into bytes.  As
  32.  *             of JDK 1.1, the preferred way to create a stream from a
  33.  *             string is via the <code>StringReader</code> class.
  34.  */
  35. public
  36. class StringBufferInputStream extends InputStream {
  37.     /**
  38.      * The string from which bytes are read. 
  39.      *
  40.      * @since      JDK1.0
  41.      */
  42.     protected String buffer;
  43.  
  44.     /**
  45.      * The index of the next character to read from the input stream buffer.
  46.      *
  47.      * @see        java.io.StringBufferInputStream#buffer
  48.      * @since      JDK1.0
  49.      */
  50.     protected int pos;
  51.  
  52.     /**
  53.      * The number of valid characters in the input stream buffer. 
  54.      *
  55.      * @see        java.io.StringBufferInputStream#buffer
  56.      * @since      JDK1.0
  57.      */
  58.     protected int count;
  59.  
  60.     /**
  61.      * Creates a string input stream to read data from the specified string.
  62.      *
  63.      * @param      s   the underlying input buffer.
  64.      * @since      JDK1.0
  65.      */
  66.     public StringBufferInputStream(String s) {
  67.     this.buffer = s;
  68.     count = s.length();
  69.     }
  70.  
  71.     /**
  72.      * Reads the next byte of data from this input stream. The value 
  73.      * byte is returned as an <code>int</code> in the range 
  74.      * <code>0</code> to <code>255</code>. If no byte is available 
  75.      * because the end of the stream has been reached, the value 
  76.      * <code>-1</code> is returned. 
  77.      * <p>
  78.      * The <code>read</code> method of 
  79.      * <code>StringBufferInputStream</code> cannot block. It returns the 
  80.      * low eight bits of the next character in this input stream's buffer. 
  81.      *
  82.      * @return     the next byte of data, or <code>-1</code> if the end of the
  83.      *             stream is reached.
  84.      * @since      JDK1.0
  85.      */
  86.     public synchronized int read() {
  87.     return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
  88.     }
  89.  
  90.     /**
  91.      * Reads up to <code>len</code> bytes of data from this input stream 
  92.      * into an array of bytes. 
  93.      * <p>
  94.      * The <code>read</code> method of 
  95.      * <code>StringBufferInputStream</code> cannot block. It copies the 
  96.      * low eight bits from the characters in this input stream's buffer into 
  97.      * the byte array argument. 
  98.      *
  99.      * @param      b     the buffer into which the data is read.
  100.      * @param      off   the start offset of the data.
  101.      * @param      len   the maximum number of bytes read.
  102.      * @return     the total number of bytes read into the buffer, or
  103.      *             <code>-1</code> if there is no more data because the end of
  104.      *             the stream has been reached.
  105.      * @since      JDK1.0
  106.      */
  107.     public synchronized int read(byte b[], int off, int len) {
  108.     if (pos >= count) {
  109.         return -1;
  110.     }
  111.     if (pos + len > count) {
  112.         len = count - pos;
  113.     }
  114.     if (len <= 0) {
  115.         return 0;
  116.     }
  117.     String    s = buffer;
  118.     int cnt = len;
  119.     while (--cnt >= 0) {
  120.         b[off++] = (byte)s.charAt(pos++);
  121.     }
  122.  
  123.     return len;
  124.     }
  125.  
  126.     /**
  127.      * Skips <code>n</code> bytes of input from this input stream. Fewer 
  128.      * bytes might be skipped if the end of the input stream is reached. 
  129.      *
  130.      * @param      n   the number of bytes to be skipped.
  131.      * @return     the actual number of bytes skipped.
  132.      * @since      JDK1.0
  133.      */
  134.     public synchronized long skip(long n) {
  135.     if (n < 0) {
  136.         return 0;
  137.     }
  138.     if (n > count - pos) {
  139.         n = count - pos;
  140.     }
  141.     pos += n;
  142.     return n;
  143.     }
  144.  
  145.     /**
  146.      * Returns the number of bytes that can be read from the input 
  147.      * stream without blocking. 
  148.      *
  149.      * @return     the value of <code>count - pos</code>, which is the
  150.      *             number of bytes remaining to be read from the input buffer. 
  151.      * @since      JDK1.0
  152.      */
  153.     public synchronized int available() {
  154.     return count - pos;
  155.     }
  156.  
  157.     /**
  158.      * Resets the input stream to begin reading from the first character 
  159.      * of this input stream's underlying buffer. 
  160.      *
  161.      * @since      JDK1.0
  162.      */
  163.     public synchronized void reset() {
  164.     pos = 0;
  165.     }
  166. }
  167.